//
//  iAdInterstitial.m
//  PaintWar
//
//  Created by Tai Le on 10/9/14.
//
//

#import "AdiAdInterstitial.h"

@implementation AdiAdInterstitial

- (id)init
{
    self = [super init];
    if (self)
    {
        [self cycleAds];
    }
    
    return self;
}

- (BOOL)showAdInterstitialInViewController:(UIViewController *)controller
{
    if (controller == nil)
    {
        return NO;
    }
    
    if (_iAdInterstitial.loaded)
    {
        [_iAdInterstitial presentFromViewController:controller];
        
        return YES;
    }
    
    return NO;
}

- (BOOL)showAdInterstitialInView:(UIView *)view
{
    if (view == nil)
    {
        return NO;
    }
    
    if (_iAdInterstitial.loaded)
    {
        [_iAdInterstitial presentInView:view];
        
        return YES;
    }
    
    return NO;
}

- (void)cycleAds
{
    [super cycleAds];
    
    _iAdInterstitial.delegate = nil;
    
    _iAdInterstitial = [[ADInterstitialAd alloc] init];
    _iAdInterstitial.delegate = self;
}

- (id)getAds
{
    return _iAdInterstitial;
}

- (void)releaseAds
{
    [super releaseAds];
    
    _iAdInterstitial.delegate = nil;
    _iAdInterstitial = nil;
}

#pragma mark - Delegate
//did load ad content, ready to show ad
- (void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd
{
    [self raiseDidLoad:interstitialAd];
}

/*!
 * @method interstitialAdDidUnload:
 *
 * @discussion
 * When this method is invoked, if the application is using -presentInView:, the
 * content will be unloaded from the container shortly after this method is
 * called and no new content will be loaded. This may occur either as a result
 * of user actions or if the ad content has expired.
 *
 * In the case of an interstitial presented via -presentInView:, the layout of
 * the app should be updated to reflect that an ad is no longer visible. e.g.
 * by removing the view used for presentation and replacing it with another view.
 */
- (void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd
{
    [self raiseDidUnload:interstitialAd];
}

/*!
 * @method interstitialAd:didFailWithError:
 *
 * @discussion
 * Called when an error has occurred attempting to get ad content.
 *
 * @see ADError for a list of possible error codes.
 */
- (void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
{    
    [self raiseDidFail:interstitialAd error:error];
}

- (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd
{
    [self raiseDidUnload:interstitialAd];
}

@end
